package com.zhang.order.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.file.entity.Files;
import com.zhang.file.service.IFileService;
import com.zhang.member.entity.MemberUser;
import com.zhang.member.service.IMemberUserService;
import com.zhang.member.service.IUserPostsService;
import com.zhang.order.dto.AppraiseSearchDTO;
import com.zhang.order.entity.AppraiseFile;
import com.zhang.order.entity.GoodsAppraise;
import com.zhang.order.entity.Order;
import com.zhang.order.mapper.GoodsAppraiseMapper;
import com.zhang.order.service.AppraiseFileService;
import com.zhang.order.service.IGoodsAppraiseService;
import com.zhang.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.order.vo.AppraiseCountVO;
import com.zhang.order.vo.OrderAppraiseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品评价表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2024-04-20
 */
@Service
public class GoodsAppraiseServiceImpl extends ServiceImpl<GoodsAppraiseMapper, GoodsAppraise> implements IGoodsAppraiseService {
    @Autowired
    private IMemberUserService userService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserPostsService userPostsService;
    @Autowired
    private IFileService fileService;
    @Autowired
    private AppraiseFileService appraiseFileService;

    @Override
    public void saveAppraise(List<OrderAppraiseVo> orderAppraiseVos) {
        MemberUser user = userService.getById(StpUtil.getLoginIdAsLong());
        orderAppraiseVos.forEach(item -> {
            GoodsAppraise goodsAppraise = new GoodsAppraise();
            goodsAppraise.setComment(item.getComment());
            goodsAppraise.setRating(item.getRating());
            goodsAppraise.setOrderItemId(item.getOrderItemId());
            goodsAppraise.setSkuId(item.getSkuId());
            goodsAppraise.setUserId(StpUtil.getLoginIdAsLong());
            goodsAppraise.setUserName(user.getUserNickname());
            goodsAppraise.setSkuImg(Long.valueOf(item.getSkuImg()));
            goodsAppraise.setOrderId(item.getOrderId());
            goodsAppraise.setSkuSpecs(item.getSkuSpecs());
            goodsAppraise.setUserAvatar(user.getAvatar());
//            goodsAppraise.setSpuId();
            save(goodsAppraise);
            //            保存用户上传的文件
            if (item.getFileIds()!=null || item.getFileIds().size()>0){
                System.out.println("测试能否拿到goodsAppraiseId?"+goodsAppraise.getId());
                item.getFileIds().forEach(fileId->{
                    Files byId = fileService.getById(fileId);
                    appraiseFileService.save(new AppraiseFile(Integer.valueOf(fileId),Integer.valueOf(goodsAppraise.getId().toString()),byId.getFileType()));
                });
            }
            //        修改订单状态
            Order order = new Order();
            order.setId(item.getOrderId());
            orderService.orderFinish(order);
        });
    }

    @Override
    public Page<GoodsAppraise> listBySkuId(String skuId, int pageNum, int pageSize) {
        LambdaQueryWrapper<GoodsAppraise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsAppraise::getSkuId, skuId);
        //通过点赞量进行排序
        wrapper.orderBy(true,true,GoodsAppraise::getLikeCount);
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public AppraiseCountVO getCountBySkuId(AppraiseSearchDTO dto) {
        AppraiseCountVO countBySkuId = baseMapper.getCountBySkuId(dto);
        LambdaQueryWrapper<GoodsAppraise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsAppraise::getSkuId,dto.getSkuId());
        if (dto.getRating()!=-1){
            wrapper.eq(GoodsAppraise::getRating,dto.getRating());
        }
        if (countBySkuId!=null){
            Page<GoodsAppraise> page = page(new Page<>(dto.getPageNum(), dto.getPageSize()), wrapper);
            for (GoodsAppraise appraise : page.getRecords()) {
                List<AppraiseFile> appraiseFiles = appraiseFileService.listByAppraiseId(appraise.getId());
                appraise.setAppraiseFiles(appraiseFiles);
            }
            countBySkuId.setPage(page);
        }
        return countBySkuId;
    }
}
